[FFmpeg-devel] [PATCH v14 1/2] avformat/imf: Demuxer
Zane van Iperen
zane at zanevaniperen.com
Sat Dec 25 02:07:50 EET 2021
Looks mostly alright, just some style nits. Once they're fixed, lgtm.
On Wednesday, 22 December 2021 4:42:08 AM AEST pal at sandflow.com wrote:
> +
> +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]);
I'm not the biggest fan of this, but if you're doing the libuuid refactoring afterwards, then I'm inclined
to let it be.
> +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)) {
When doing inline assignments, use an extra (), e.g.
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 (ret = ff_imf_xml_read_uuid(track_id_elem, uuid)) {
()
Or if the return value isn't actually used, just remove the assignment.
> + 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*/
Nit: Missing a space.
> + 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 (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;
> + } else {
> + 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;
> + }
> +
> + 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);
> + }
> +
> + av_bprint_finalize(&buf, NULL);
> +
> + return ret;
> +}
> diff --git a/libavformat/imfdec.c b/libavformat/imfdec.c
> new file mode 100644
> index 0000000000..614bf6ab1f
> --- /dev/null
> +++ b/libavformat/imfdec.c
> @@ -0,0 +1,899 @@
> +/*
> + * 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.
> + */
> +
> +/**
> + * Demuxes an IMF Composition
> + *
> + * References
> + * OV 2067-0:2018 - SMPTE Overview Document - Interoperable Master Format
> + * ST 2067-2:2020 - SMPTE Standard - Interoperable Master Format — Core Constraints
> + * ST 2067-3:2020 - SMPTE Standard - Interoperable Master Format — Composition Playlist
> + * ST 2067-5:2020 - SMPTE Standard - Interoperable Master Format — Essence Component
> + * ST 2067-20:2016 - SMPTE Standard - Interoperable Master Format — Application #2
> + * ST 2067-21:2020 - SMPTE Standard - Interoperable Master Format — Application #2 Extended
> + * ST 2067-102:2017 - SMPTE Standard - Interoperable Master Format — Common Image Pixel Color Schemes
> + * ST 429-9:2007 - SMPTE Standard - D-Cinema Packaging — Asset Mapping and File Segmentation
> + *
> + * @author Marc-Antoine Arnaud
> + * @author Valentin Noel
> + * @author Nicholas Vanderzwet
> + * @file
> + * @ingroup lavu_imf
> + */
> +
> +#include "avio_internal.h"
> +#include "imf.h"
> +#include "internal.h"
> +#include "libavutil/avstring.h"
> +#include "libavutil/bprint.h"
> +#include "libavutil/opt.h"
> +#include "mxf.h"
> +#include "url.h"
> +#include <inttypes.h>
> +#include <libxml/parser.h>
> +
> +#define MAX_BPRINT_READ_SIZE (UINT_MAX - 1)
> +#define DEFAULT_ASSETMAP_SIZE 8 * 1024
> +#define AVRATIONAL_FORMAT "%d/%d"
> +#define AVRATIONAL_ARG(rational) rational.num, rational.den
> +
> +/**
> + * IMF Asset locator
> + */
> +typedef struct IMFAssetLocator {
> + FFIMFUUID uuid;
> + char *absolute_uri;
> +} IMFAssetLocator;
> +
> +/**
> + * IMF Asset locator map
> + * Results from the parsing of one or more ASSETMAP XML files
> + */
> +typedef struct IMFAssetLocatorMap {
> + uint32_t asset_count;
> + IMFAssetLocator *assets;
> +} IMFAssetLocatorMap;
> +
> +typedef struct IMFVirtualTrackResourcePlaybackCtx {
> + IMFAssetLocator *locator;
> + FFIMFTrackFileResource *resource;
> + AVFormatContext *ctx;
> +} IMFVirtualTrackResourcePlaybackCtx;
> +
> +typedef struct IMFVirtualTrackPlaybackCtx {
> + int32_t index; /**< Track index in playlist */
> + AVRational current_timestamp; /**< Current temporal position */
> + AVRational duration; /**< Overall duration */
> + uint32_t resource_count; /**< Number of resources */
> + unsigned int resources_alloc_sz; /**< Size of the buffer holding the resource */
> + IMFVirtualTrackResourcePlaybackCtx *resources; /**< Buffer holding the resources */
> + uint32_t current_resource_index; /**< Current resource */
> + int64_t last_pts; /**< Last timestamp */
> +} IMFVirtualTrackPlaybackCtx;
> +
> +typedef struct IMFContext {
> + const AVClass *class;
> + const char *base_url;
> + char *asset_map_paths;
> + AVIOInterruptCB *interrupt_callback;
> + AVDictionary *avio_opts;
> + FFIMFCPL *cpl;
> + IMFAssetLocatorMap asset_locator_map;
> + uint32_t track_count;
> + IMFVirtualTrackPlaybackCtx **tracks;
> +} IMFContext;
> +
> +static int imf_uri_is_url(const char *string)
> +{
> + return strstr(string, "://") != NULL;
> +}
> +
> +static int imf_uri_is_unix_abs_path(const char *string)
> +{
> + return string[0] == '/';
> +}
> +
> +static int imf_uri_is_dos_abs_path(const char *string)
> +{
> + /* Absolute path case: `C:\path\to\somwhere` */
> + if (string[1] == ':' && string[2] == '\\')
> + return 1;
> +
> + /* Absolute path case: `C:/path/to/somwhere` */
> + if (string[1] == ':' && string[2] == '/')
> + return 1;
> +
> + /* Network path case: `\\path\to\somwhere` */
> + if (string[0] == '\\' && string[1] == '\\')
> + return 1;
> +
> + return 0;
> +}
> +
> +/**
> + * Parse a ASSETMAP XML file to extract the UUID-URI mapping of assets.
> + * @param s the current format context, if any (can be NULL).
> + * @param doc the XML document to be parsed.
> + * @param asset_map pointer on the IMFAssetLocatorMap to fill.
> + * @param base_url the url of the asset map XML file, if any (can be NULL).
> + * @return a negative value in case of error, 0 otherwise.
> + */
> +static int parse_imf_asset_map_from_xml_dom(AVFormatContext *s,
> + xmlDocPtr doc,
> + IMFAssetLocatorMap *asset_map,
> + const char *base_url)
> +{
> + xmlNodePtr asset_map_element = NULL;
> + xmlNodePtr node = NULL;
> + xmlNodePtr asset_element = NULL;
> + unsigned long elem_count;
> + char *uri;
> + int ret = 0;
> + IMFAssetLocator *asset = NULL;
> + void *tmp;
> +
> + asset_map_element = xmlDocGetRootElement(doc);
> +
> + if (!asset_map_element) {
> + av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing root node\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + if (asset_map_element->type != XML_ELEMENT_NODE || av_strcasecmp(asset_map_element->name, "AssetMap")) {
> + av_log(s,
> + AV_LOG_ERROR,
> + "Unable to parse asset map XML - wrong root node name[%s] type[%d]\n",
> + asset_map_element->name,
> + (int)asset_map_element->type);
> + return AVERROR_INVALIDDATA;
> + }
> +
> + /* parse asset locators */
> + if (!(node = ff_imf_xml_get_child_element_by_name(asset_map_element, "AssetList"))) {
> + av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing AssetList node\n");
> + return AVERROR_INVALIDDATA;
> + }
> + elem_count = xmlChildElementCount(node);
> + if (elem_count > UINT32_MAX
> + || asset_map->asset_count > UINT32_MAX - elem_count)
> + return AVERROR(ENOMEM);
> + tmp = av_realloc_array(asset_map->assets,
> + elem_count + asset_map->asset_count,
> + sizeof(IMFAssetLocator));
> + if (!tmp) {
> + av_log(NULL, AV_LOG_ERROR, "Cannot allocate IMF asset locators\n");
> + return AVERROR(ENOMEM);
> + }
> + asset_map->assets = tmp;
> +
> + asset_element = xmlFirstElementChild(node);
> + while (asset_element) {
> + if (av_strcasecmp(asset_element->name, "Asset") != 0)
> + continue;
> +
> + asset = &(asset_map->assets[asset_map->asset_count]);
> +
> + if (ff_imf_xml_read_uuid(ff_imf_xml_get_child_element_by_name(asset_element, "Id"), asset->uuid)) {
> + av_log(s, AV_LOG_ERROR, "Could not parse UUID from asset in asset map.\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + av_log(s, AV_LOG_DEBUG, "Found asset id: " FF_IMF_UUID_FORMAT "\n", UID_ARG(asset->uuid));
> +
> + if (!(node = ff_imf_xml_get_child_element_by_name(asset_element, "ChunkList"))) {
> + av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing ChunkList node\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + if (!(node = ff_imf_xml_get_child_element_by_name(node, "Chunk"))) {
> + av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing Chunk node\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + uri = xmlNodeGetContent(ff_imf_xml_get_child_element_by_name(node, "Path"));
> + if (!imf_uri_is_url(uri) && !imf_uri_is_unix_abs_path(uri) && !imf_uri_is_dos_abs_path(uri))
> + asset->absolute_uri = av_append_path_component(base_url, uri);
> + else
> + asset->absolute_uri = av_strdup(uri);
> + xmlFree(uri);
> + if (!asset->absolute_uri)
> + return AVERROR(ENOMEM);
> +
> + av_log(s, AV_LOG_DEBUG, "Found asset absolute URI: %s\n", asset->absolute_uri);
> +
> + asset_map->asset_count++;
> + asset_element = xmlNextElementSibling(asset_element);
> + }
> +
> + return ret;
> +}
> +
> +/**
> + * Initializes an IMFAssetLocatorMap structure.
> + */
> +static void imf_asset_locator_map_init(IMFAssetLocatorMap *asset_map)
> +{
> + asset_map->assets = NULL;
> + asset_map->asset_count = 0;
> +}
> +
> +/**
> + * Free a IMFAssetLocatorMap pointer.
> + */
> +static void imf_asset_locator_map_deinit(IMFAssetLocatorMap *asset_map)
> +{
> + for (uint32_t i = 0; i < asset_map->asset_count; ++i)
> + av_freep(&asset_map->assets[i].absolute_uri);
> +
> + av_freep(&asset_map->assets);
> +}
> +
> +static int parse_assetmap(AVFormatContext *s, const char *url)
> +{
> + IMFContext *c = s->priv_data;
> + AVIOContext *in = NULL;
> + struct AVBPrint buf;
> + AVDictionary *opts = NULL;
> + xmlDoc *doc = NULL;
> + const char *base_url;
> + char *tmp_str = NULL;
> + int ret;
> + int64_t filesize;
> +
> + av_log(s, AV_LOG_DEBUG, "Asset Map URL: %s\n", url);
> +
> + av_dict_copy(&opts, c->avio_opts, 0);
> + ret = s->io_open(s, &in, url, AVIO_FLAG_READ, &opts);
> + av_dict_free(&opts);
> + if (ret < 0)
> + return ret;
> +
> + filesize = avio_size(in);
> + filesize = filesize > 0 ? filesize : DEFAULT_ASSETMAP_SIZE;
> +
> + av_bprint_init(&buf, filesize + 1, AV_BPRINT_SIZE_UNLIMITED);
> +
> + ret = avio_read_to_bprint(in, &buf, MAX_BPRINT_READ_SIZE);
> + if (ret < 0 || !avio_feof(in) || buf.len == 0) {
> + av_log(s, AV_LOG_ERROR, "Unable to read to asset map '%s'\n", url);
> + if (ret == 0)
> + ret = AVERROR_INVALIDDATA;
> + goto clean_up;
> + }
> +
> + LIBXML_TEST_VERSION
> +
> + tmp_str = av_strdup(url);
> + if (!tmp_str) {
> + ret = AVERROR(ENOMEM);
> + goto clean_up;
> + }
> + base_url = av_dirname(tmp_str);
> +
> + filesize = buf.len;
> + doc = xmlReadMemory(buf.str, filesize, url, NULL, 0);
> +
> + ret = parse_imf_asset_map_from_xml_dom(s, doc, &c->asset_locator_map, base_url);
> + if (!ret)
> + av_log(s,
> + AV_LOG_DEBUG,
> + "Found %d assets from %s\n",
> + c->asset_locator_map.asset_count,
> + url);
> +
> + xmlFreeDoc(doc);
> +
> +clean_up:
> + if (tmp_str)
> + av_freep(&tmp_str);
> + ff_format_io_close(s, &in);
> + av_bprint_finalize(&buf, NULL);
> + return ret;
> +}
> +
> +static IMFAssetLocator *find_asset_map_locator(IMFAssetLocatorMap *asset_map, FFIMFUUID uuid)
> +{
> + for (uint32_t i = 0; i < asset_map->asset_count; ++i) {
> + if (memcmp(asset_map->assets[i].uuid, uuid, 16) == 0)
> + return &(asset_map->assets[i]);
> + }
> + return NULL;
> +}
> +
> +static int open_track_resource_context(AVFormatContext *s,
> + IMFVirtualTrackResourcePlaybackCtx *track_resource)
> +{
> + IMFContext *c = s->priv_data;
> + int ret = 0;
> + int64_t entry_point;
> + AVDictionary *opts = NULL;
> +
> + if (track_resource->ctx) {
> + av_log(s,
> + AV_LOG_DEBUG,
> + "Input context already opened for %s.\n",
> + track_resource->locator->absolute_uri);
> + return 0;
> + }
> +
> + track_resource->ctx = avformat_alloc_context();
> + if (!track_resource->ctx)
> + return AVERROR(ENOMEM);
> +
> + track_resource->ctx->io_open = s->io_open;
> + track_resource->ctx->io_close = s->io_close;
> + track_resource->ctx->io_close2 = s->io_close2;
> + track_resource->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
> +
> + if ((ret = ff_copy_whiteblacklists(track_resource->ctx, s)) < 0)
> + goto cleanup;
> +
> + if (ret = av_opt_set(track_resource->ctx, "format_whitelist", "mxf", 0))
()
I stopped here, there's probably more.
More information about the ffmpeg-devel
mailing list